﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using TO3.Models;
using TO3.DAL;

namespace TO3.Controllers
{
    public class RegistrationController : Controller
    {
        private TournamentContext db = new TournamentContext();

        // GET: /Registration/
        public ActionResult Index()
        {
            //Get the active tournamentId (if any)
            int tournamentId;

            if (Session["ActiveTournamentId"] == null)
            {
                tournamentId = 0;
            }
            else
            {
                tournamentId = (int)Session["ActiveTournamentId"];
                //Get the tournament name            
                ViewBag.TournamentName = db.Tournaments.Single(t => t.Id == tournamentId).Name;
            }

            //Check if the logged in user is an organiser or admin
            if (User.IsInRole("Organiser") || User.IsInRole("Admin"))
            {
                ViewBag.IsOrganiser = true;
            }
            else
            {
                ViewBag.IsOrganiser = false;
            }

            //Only display the registrations for the current tournament
            var tournamentregistrations = db.TournamentRegistrations.Include(t => t.Player).Include(t => t.Tournament).Where(t => t.TournamentId == tournamentId);

            return View(tournamentregistrations.ToList());
        }

        // GET: /Registration/Details/5
        public ActionResult Details(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            TournamentRegistration tournamentregistration = db.TournamentRegistrations.Find(id);
            if (tournamentregistration == null)
            {
                return HttpNotFound();
            }
            return View(tournamentregistration);
        }

        // GET: /Registration/Create
        public ActionResult Create()
        {
            //Check if the logged in user is an organiser or admin
            if (User.IsInRole("Organiser") || User.IsInRole("Admin"))
            {
                ViewBag.IsOrganiser = true;
                ViewBag.PlayerId = new SelectList(db.Players, "Id", "Name");
            }
            else
            {
                ViewBag.IsOrganiser = false;

                //Get the user's playerId
                ApplicationDbContext userContext = new ApplicationDbContext();
                var user = userContext.Users.Single(u => u.UserName == User.Identity.Name);

                ViewBag.PlayerId = new SelectList(db.Players.Where(p => p.Id == user.PlayerID), "Id", "Name");
            }
            
            ViewBag.TournamentId = new SelectList(db.Tournaments, "Id", "Name");
            return View();
        }

        // POST: /Registration/Create
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create([Bind(Include="Id,TournamentId,PlayerId,HasPaid")] TournamentRegistration tournamentregistration)
        {
            if (ModelState.IsValid)
            {
                db.TournamentRegistrations.Add(tournamentregistration);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            ViewBag.PlayerId = new SelectList(db.Players, "Id", "Name", tournamentregistration.PlayerId);
            ViewBag.TournamentId = new SelectList(db.Tournaments, "Id", "Name", tournamentregistration.TournamentId);
            return View(tournamentregistration);
        }

        // GET: /Registration/Edit/5
        [Authorize(Roles = "Organiser, Admin")]
        public ActionResult Edit(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            TournamentRegistration tournamentregistration = db.TournamentRegistrations.Find(id);
            if (tournamentregistration == null)
            {
                return HttpNotFound();
            }
            ViewBag.PlayerId = new SelectList(db.Players, "Id", "Name", tournamentregistration.PlayerId);
            ViewBag.TournamentId = new SelectList(db.Tournaments, "Id", "Name", tournamentregistration.TournamentId);
            return View(tournamentregistration);
        }

        // POST: /Registration/Edit/5
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        [Authorize(Roles = "Organiser, Admin")]
        public ActionResult Edit([Bind(Include="Id,TournamentId,PlayerId,HasPaid")] TournamentRegistration tournamentregistration)
        {
            if (ModelState.IsValid)
            {
                db.Entry(tournamentregistration).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.PlayerId = new SelectList(db.Players, "Id", "Name", tournamentregistration.PlayerId);
            ViewBag.TournamentId = new SelectList(db.Tournaments, "Id", "Name", tournamentregistration.TournamentId);
            return View(tournamentregistration);
        }

        // GET: /Registration/Delete/5
        [Authorize(Roles = "Organiser, Admin")]
        public ActionResult Delete(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            TournamentRegistration tournamentregistration = db.TournamentRegistrations.Find(id);
            if (tournamentregistration == null)
            {
                return HttpNotFound();
            }
            return View(tournamentregistration);
        }

        // POST: /Registration/Delete/5
        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        [Authorize(Roles = "Organiser, Admin")]
        public ActionResult DeleteConfirmed(int id)
        {
            TournamentRegistration tournamentregistration = db.TournamentRegistrations.Find(id);
            db.TournamentRegistrations.Remove(tournamentregistration);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);
        }
    }
}
